<template>
  <div v-if="isAdd == 1" class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="订单编号" prop="orderNo">
              <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="加工时间" prop="operatorTime">
              <el-input v-model="queryParams.operatorTime" placeholder="请输入加工时间" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="审核状态" prop="auditStatus">
              <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
                <el-option v-for="dict in order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card v-if="isAdd == 1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm:process:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm:process:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm:process:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['farm:process:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="订单编号" align="center" prop="orderNo" />
        <el-table-column label="自制料id" align="center" prop="productId" />
        <el-table-column label="自制料单价" align="center" prop="price" />
        <el-table-column label="总数" align="center" prop="totalNum" />
        <el-table-column label="总价格" align="center" prop="totalMoney" />
        <el-table-column label="加工时间" align="center" prop="operatorTime" />
        <el-table-column label="审核状态" align="center" prop="auditStatus">
          <template #default="scope">
            <dict-tag :options="order_status" :value="scope.row.auditStatus" />
          </template>
        </el-table-column>
        <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
          <template #default="scope">
            <span>{{ scope.row.auditTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核人" align="center" prop="auditBy" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['farm:process:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['farm:process:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
  <!-- 审核对话框 -->
  <Audit v-model="dialog.visible" :row-data="rowData" @load-list="getList" @ok="sonData" />
  <!-- 新增/编辑 -->
  <CommonProcessAdd v-if="isAdd == 2" :row-data="rowData" @close-add="closeAdd" @get-list="getList" />
</template>

<script setup name="Process" lang="ts">
import { listProcess, getProcess, delProcess, addProcess, updateProcess } from '@/api/farm/common/process';
import { ProcessVO, ProcessQuery, ProcessForm } from '@/api/farm/common/process/types';
import Audit from '@/components/Audit/index.vue';
import CommonProcessAdd from '@/views/farm/common/process/add.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { order_status } = toRefs<any>(proxy?.useDict('order_status'));

const processList = ref<ProcessVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const isAdd = ref<number>(1);
const total = ref(0);
const rowData = ref({});
const queryFormRef = ref<ElFormInstance>();
const processFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const data = reactive<any>({
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    orderNo: undefined,
    operatorTime: undefined,
    auditStatus: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(data);
/*打开审核窗口*/
const handleAudit = (e) => {
  rowData.value = e;
  dialog.visible = true;
  dialog.title = '审核';
};
/** 查询饲料加工列表 */
const getList = async () => {
  loading.value = true;
  const res = await listProcess(queryParams.value);
  processList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};


/*关闭新增*/
const closeAdd = (e) => {
  isAdd.value = e;
};


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: ProcessVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  isAdd.value = 2;
};

/** 修改按钮操作 */
const handleUpdate = async (row?: ProcessVO) => {
  isAdd.value = 2;
  const _id = row?.id || ids.value[0];
  const res = await getProcess(_id);
  rowData.value = res.data;
};
/*当子组件有值时执行审核*/
const sonData = (e) => {
  if (e) {
    const audit = async () => {
      let res = await auditChickenFeedOutApi(e);
      if (res.code === 200) {
        proxy?.$modal.msgSuccess('审核成功');
        await getList();
      }
    };
    audit();
  }
};


/** 删除按钮操作 */
const handleDelete = async (row?: ProcessVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除饲料加工编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delProcess(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'farm/process/export',
    {
      ...queryParams.value
    },
    `process_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
